package com.ldaptest;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class SearchUser {
    public void searchUser() {
        Hashtable env = new Hashtable(11);
        String ldapURL = "ldap://172.16.1.1:389";
        String adminName = "CN=Administrator,CN=Users,DC=SCRC,DC=COM,DC=CN";
        String adminPassword = "huangzhongchenlisunliang";
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, adminName);// User
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);// Password
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, ldapURL); // connect to my domain
                                                // controller
        env.put(Context.PROVIDER_URL, ldapURL);
        try { // Create the initial directory context
            LdapContext ctx = new InitialLdapContext(env, null);
            // Create the search controls

            // Specify the search scope
            SearchControls searchCtls = new SearchControls();

            // specify the LDAP search filter
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String searchFilter = "(&(objectClass=User))";
            // String searchFilter = "(&(objectClass=member))";
            // String searchFilter = "(&(objectClass=group))";
            // Specify the Base for the search

            // initialize counter to total the group members
            String searchBase = "DC=scrc,DC=com,DC=cn";
            int totalResults = 0; // Specify the attributes to return
            String returnedAtts[] = { "memberOf" };

            // Search for objects using the filter
            searchCtls.setReturningAttributes(returnedAtts);
            // Loop through the search results
            NamingEnumeration answer = ctx.search(searchBase, searchFilter, searchCtls);
            while (answer.hasMoreElements()) {
                SearchResult sr = (SearchResult) answer.next();

                // Print out the groups
                System.out.println(">>>" + sr.getName());
                Attributes attrs = sr.getAttributes();
                if (attrs != null) {
                    try {
                        for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                            Attribute attr = (Attribute) ae.next();
                            System.out.println("Attribute: " + attr.getID());
                            for (NamingEnumeration e = attr.getAll(); e.hasMore(); totalResults++) {
                                System.out.println(" " + totalResults + ". " + e.next());
                            }
                        }
                    } catch (NamingException e) {
                        System.err.println("Problem listing membership: " + e);
                    }
                }
            }
            System.out.println("Total groups: " + totalResults);
            ctx.close();
        } catch (NamingException e) {
            e.printStackTrace();
            System.err.println("Problem searching directory: " + e);
        }
    }
}
